" =============
" 常规设置
" =============
let mapleader=" "

syntax on         " 开启语法高亮
set number        " 显示行号
set cursorline    " 高亮显示当前行
set cursorcolumn  " 高亮显示当前列
set wrap          " 自动换行
set showcmd       " 显示指令
set wildmenu      " 补全菜单
set hlsearch      " 高亮搜索
set incsearch     " 动态高亮
set ignorecase    " 不区分大小写
set smartcase     " 智能大小写
set nocompatible  " 丢弃Vi功能
filetype on
filetype indent on
filetype plugin on
filetype plugin indent on

set encoding=utf-8 " 文件编码
let &t_ut=''       " 有些终端可能配色不行
set expandtab      " 下面四行，是把缩进改成2割空格
set tabstop=4 
set shiftwidth=4 
set softtabstop=4
set scrolloff=3    " 光标最多到达倒数第三行
set tw=0
set indentexpr=
set backspace=indent,eol,start  " 退格键可以从行头退到行尾
set foldmethod=manual " 代码折叠有关
set foldlevel=99
let &t_SI = "\<Esc>]50;CursorShape=1\x7"    " 光标样式有关
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
set laststatus=2                " 状态栏=2
set autochdir                   " Vim执行东西在当前目录
set undofile                    " 持久撤销
set undodir=~/.temp             " 撤销的文件路径
set showmatch                   " 显示括号匹配
set clipboard=unnamedplus       " 设置是否和系统共用一个剪贴板
set signcolumn=yes              " !!!错误提示栏
set hidden

" set relativenumber " 显示从当前行数的前后行数
" set mouse=a


" =============
" 基础键位设置
" =============
inoremap jk <ESC>
vnoremap N :norm
vnoremap ,y "+y
vnoremap ,x "+ygvd

noremap R :source $MYVIMRC<CR>
noremap Q :q<CR>
noremap sV <c-w>t<C-w>H   " 水平分屏切换为竖直分屏"
noremap sH <C-w>t<C-w>K   " 竖直分屏切换为水平分屏"

noremap S :w<CR>
" 标签页的操作
noremap EN :tabe<CR>  
noremap El :+tabnext<CR>
noremap Eh :-tabnext<CR>
noremap <leader>x :bd<CR>
" 快速移动
noremap J 5j          
noremap K 5k
noremap L 7l
noremap H 7h
noremap j gj
noremap k gk
noremap ,j J
noremap ,c ggVG"+y
noremap sd "_d
noremap Y yyp
" 快速到配置文件
noremap <LEADER><CR> :nohlsearch<CR>
noremap <LEADER>rc :e ~/.config/nvim/init.vim<CR>
noremap <leader>al :e ~/.config/alacritty/alacritty.yml<CR>
noremap <leader>nb :e ~/.bashrc<CR>
" 分屏操作
noremap <LEADER>h <C-w>h
noremap <LEADER>j <C-w>j
noremap <LEADER>k <C-w>k
noremap <LEADER>l <C-w>l
noremap <up> :res -5<CR>
noremap <down> :res +5<CR>
noremap <right> :vertical resize+5<CR>
noremap <left> :vertical resize-5<CR>
noremap sh :set nosplitright<CR>:vsplit<CR>
noremap sl :set splitright<CR>:vsplit<CR>
noremap sj :set splitbelow<CR>:split<CR>
noremap sk :set nosplitbelow<CR>:split<CR>
noremap <C-w> <C-a>
noremap sq :q!<CR>
tnoremap <ESC> <C-\><C-n>



" 异步运行
noremap ,q :call CompileRunGcc()<CR>
func! CompileRunGcc()
  exec "w"
  if &filetype == 'c'
    exec "!g++ % -o %<"
    :sp
    :term ./%<
  elseif &filetype == 'cpp'
    set splitbelow
    exec "!g++ -std=c++11 % -Wall -o %<"
    :sp
    :term ./%<
  elseif &filetype == 'java'
    exec "!javac %"
    set splitbelow
    :sp
    :term java %:t:r
  elseif &filetype == 'sh'
    :!time bash %
  elseif &filetype == 'python'
    set splitbelow
    :sp
    :term python3 -W ignore %
  elseif &filetype == 'html'
    silent! exec "!".g:mkdp_browser." % &"
  elseif &filetype == 'markdown'
    exec "MarkdownPreview"
  elseif &filetype == 'tex'
    silent! exec "VimtexStop"
    silent! exec "VimtexCompile"
  elseif &filetype == 'dart'
    CocCommand flutter.run -d iPhone\ 11\ Pro
    CocCommand flutter.dev.openDevLog
  elseif &filetype == 'go'
    set splitbelow
    :sp
    :term go run .
  endif
endfunc 
